package TUILayer;

import ControlLayer.SalesCtr;
import ModelLayer.RealException;

public class SalesLineUI {
	
	private SalesCtr salesCtr;
	private InputParser inputParser;
	private int id;
	
	
	
	public SalesLineUI(InputParser inputParser) 
	{
		salesCtr = new SalesCtr();
		this.inputParser = inputParser;
	}
	
	public void start(int id) throws RealException
	{
		this.id = id;
		boolean exit = false;
		int choise = 0;
		if (salesCtr.getSales(id) != null) {
			do {
				try {
					choise = inputParser
					.getInt("\f ***SaleLinesMenu*** \n\n1. Add salesline\n2. Delete saleline\n3. Show salelineInfo\n4. List all salelines\n5. Return\nPlease make your choice: ");
				
					switch (choise) {
					case 1:
						addSalesline();
						break;
					case 2:
						deleteSalesline();
						break;
					case 3:
						showSaleslineInfo();
						break;
					case 4:
						printAllSaleSalelines();
						break;
					case 5:
						exit = true;
						break;
					}
				}
				catch (RealException e) {
						System.out.println("\n" + e.getError() + "Please try again");
						inputParser.printReturn();
				}
			}while (!exit);
		}
	}
	
	// add a saleline to a sale
	public void addSalesline() throws RealException
	{
		String barcode = inputParser.getString("\f*** AddSaleslineMenu ***\n\nPlease input the product barcode: ");
		int quantity = inputParser.getInt("\f*** AddSaleslineMenu ***\n\nPlease input the quantity of the product: ");
		salesCtr.addSalesLine(id, quantity, barcode);
		printAllSaleSalelines();
		
	}
	
	// delete a saleline to a sale
	public void deleteSalesline() throws RealException 
	{
		String barcode = inputParser.getString("\f *** DeleteSaleslineMenu ***\n\nPlease input the product barcode: ");
		salesCtr.removeSalesLine(id, barcode);
		printAllSaleSalelines();
	}
	
	// show a salelines information for a sale
	public void showSaleslineInfo() throws RealException 
	{
		String barcode = inputParser.getString("\f *** SaleslinePriceMenu ***\n\nPlease input the product barcode: ");
		System.out.println(salesCtr.getSalelineInfo(id, barcode));
		inputParser.printReturn();
		
	}
	
	// show all salelines information for a sale
	public void printAllSaleSalelines() throws RealException 
	{
		System.out.println("\f *** SaleslineMenu ***\n\n");
		if (salesCtr.getSales(id).listAllSalelines() != null) {
					System.out.println(salesCtr.getSales(id).listAllSalelines());
		}
		else {
			System.out.println("\nNo saleline has been added.");
		}
		inputParser.printReturn();
		
	}
	
}
